package com.qfjy.zhaohongyan.service.Imp;

import com.qfjy.zhaohongyan.service.ZhyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import javax.servlet.http.HttpServletRequest;
import java.util.Random;

@Slf4j
@Service
public class ZhyServiceImp implements ZhyService {
    /**简单的实现：-------------------
     * 1.获取手机号
     * 2.根据手机号 获取验证码
     * 3.获取到验证码，存入Redis;
     * 4.点击确定，与用户输入的验证码进行对比；
     * -----------------------------------
     *   2.1 验证手机号是否重复提交
     *   2.210分钟内有效
     */

    @Autowired
    private JedisPool jedisPool;
    @Override
    public void code(String phone) {
//        生成四位随机数
        Random random = new Random();
        String code = (int) (Math.random() * 9000 + 1000)+"";
        log.info("验证码："+code+"=====================================");
//        连接Redis
        Jedis jedis=jedisPool.getResource();
        String result=null;
        if(jedis.exists(phone)){
            result=jedis.get(phone);
            jedis.expire(phone,90);
            jedis.hset(phone,"code",result);
        }
        jedis.hset(phone,"code",code);
    }

    @Override
    public boolean login(String phone, String msg) {
        Jedis jedis=jedisPool.getResource();
        String code = jedis.hget(phone, "code");
        if (msg.equals(code)){
            return true;
        }
        return false;
    }

}

